package com.boco.ibssystem.common.utils;

import com.boco.ibssystem.common.exception.support.BusinessException;
import org.springframework.http.HttpStatus;
import org.springframework.validation.Errors;
import org.springframework.validation.ObjectError;

/**
 * validate验证处理工具类
 *
 * @author sunshixiong
 * @date 2018/2/2 17:43
 */
public final class BindingResultUtil {

  private BindingResultUtil() {
    throw new IllegalStateException("BindingResultUtil class");
  }

  public static void judegResult(Errors results) {
    if (results.hasErrors()) {
      StringBuilder message = new StringBuilder();
      results.getAllErrors().forEach((ObjectError result) -> {
        message.append(result.getDefaultMessage());
        message.append(",");
      });
      throw new BusinessException(HttpStatus.BAD_REQUEST.value(), message.toString());
    }
  }
}
